
import lang from '@/lang/menu/index.js'
import { addMonitor } from '@/api/app'
import router from '../permission'
import store from '@/store'
import { BEHAVIOR_ANALYSIS } from '@/store/Getter/getterTypes'

import { uuid } from '@/utils/util'
import { treeToArray } from '@/utils/array'
import storage from '@/utils/storage'

import debounce from 'throttle-debounce/debounce'


export function getScrollTop() {
  var scrollTop = 0,
    bodyScrollTop = 0,
    documentScrollTop = 0
  if (document.body) {
    bodyScrollTop = document.body.scrollTop
  }
  if (document.documentElement) {
    //兼容ie
    documentScrollTop = document.documentElement.scrollTop
  }
  scrollTop =
    bodyScrollTop - documentScrollTop > 0
      ? bodyScrollTop
      : documentScrollTop
  return scrollTop
}
//获取可滚动高度
export function getScrollHeight() {
  var scrollHeight = 0,
    bodyScrollHeight = 0,
    documentScrollHeight = 0
  if (document.body) {
    bodyScrollHeight = document.body.scrollHeight
  }
  if (document.documentElement) {
    //兼容ie
    documentScrollHeight = document.documentElement.scrollHeight
  }
  scrollHeight =
    bodyScrollHeight - documentScrollHeight > 0
      ? bodyScrollHeight
      : documentScrollHeight
  return scrollHeight
}
//获取可见区域高
export function getWindowHeight() {
  var windowHeight = 0
  if (document.compatMode == 'CSS1Compat') {
    //兼容ie
    windowHeight = document.documentElement.clientHeight
  } else {
    windowHeight = document.body.clientHeight
  }
  return windowHeight
}

export const scroll = debounce(500, () => {
  if (!store.getters[BEHAVIOR_ANALYSIS]) {
    return false
  }
  if (Math.round(getScrollTop() + getWindowHeight()) == getScrollHeight()) {
    judgeOperateTime()
    sendMonitor('scroll', '加载数据')
  }
})

//通过路由编码获取路由名称，通过多语言转换为中文
export function getTitle(routeName) {
  let routes, routesArray = []
  let routeInfo = {}
  routes = JSON.parse(JSON.stringify(store.state.routes))
  routesArray = JSON.parse(JSON.stringify(treeToArray(routes)))
  routeInfo = routesArray.find((item) => {
    return item.name == routeName
  })
  return routeInfo ? routeInfo.title ? lang[routeInfo.title] : routeInfo.name : ''
}
//最终发生行为采集数据方法
export function sendMonitor(ev, fun, md) {
  if (!storage.get('access_token')) {
    return false
  }
  let data = {
    pa: router.history.current.fullPath,
    sr: `${window.screen.height}x${window.screen.width}`,
    nt: navigator?.connection?.effectiveType,
    lan: navigator?.language,
    url: window.location.href,
    fl: store.getters.language || '',
    mc: router.history.current.name,
    mn: getTitle(router.history.current.name),
    ev: ev,
    fun: fun,
    md: JSON.stringify(md),
    depthId: storage.get('depthId')
  }
  addMonitor(data)
}
//获取时间差，以秒为单位
export function getDate(enterTime, leaveTime) {
  let date1 = leaveTime.getTime() - new Date(enterTime).getTime()
  let day = Math.round(date1 / 1000)
  // //计算出小时数
  // var leave1 = date1 % (24 * 3600 * 1000)    //计算天数后剩余的毫秒数
  // var hours = Math.floor(leave1 / (3600 * 1000))
  // //计算相差分钟数
  // var leave2 = leave1 % (3600 * 1000)        //计算小时数后剩余的毫秒数
  // var minutes = Math.floor(leave2 / (60 * 1000))
  // //计算相差秒数
  // var leave3 = leave2 % (60 * 1000)      //计算分钟数后剩余的毫秒数
  // var seconds = Math.roconsole.log()und(leave3 / 1000)
  return day
}
//更新用户操作时间、访问深度ID
export function judgeOperateTime() {
  if (storage.get('access_token')) {
    if (storage.get('operateTime')) {
      if (getDate(storage.get('operateTime'), new Date()) > 3600) {
        storage.set('depthId', uuid(16, 32))
        storage.set('operateTime', new Date())
      } else {
        storage.set('operateTime', new Date())
      }
    } else {
      storage.set('operateTime', new Date())
      if (!storage.get('depthId')) {
        storage.set('depthId', uuid(16, 32))
      }
    }
  }
}
//用户点击时触发行为采集判断方法
export function clickEvent(event) {
  if (!store.getters[BEHAVIOR_ANALYSIS]) {
    return false
  }
  //更新用户操作时间
  judgeOperateTime()
  //判断是否开启埋点

  //判断是否为下载事件，下载事件会触发两次点击只需要采集一次即可，所以过滤掉下载触发的第二次点击事件
  if (!event.target.download) {
    //判读点击元素类名数组是否为空，为空判断父节点
    if (event.target.classList.length > 0) {
      //判断类名数组中是否包含所需采集的类名
      if (judge(event.target.classList)) {
        event.target.innerText ? sendMonitor('click', event.target.innerText) : sendMonitor('click', event.target.title)
      } else {
        if (event.target.classList[0] == 'svg-icon') {
          decide(event.target.parentNode)
        }
      }
    } else {
      if (event.target.parentNode) {
        decide(event.target.parentNode)
      }
    }
  }

}

export function decide(event) {
  // 当target为document时classList为undefined
  if (event.classList?.length > 0) {
    if (judge(event.classList)) {
      event.innerText ? sendMonitor('click', event.innerText) : sendMonitor('click', event.title)
    } else {
      if (event.classList[0] == 'svg-icon') {
        decide(event.parentNode)
      }
    }
  } else {
    if (event.parentNode) {
      decide(event.parentNode)
    }
  }
}
//需要进行行为采集的类名数组，可根据项目自身需求自行增加
const classList = [
  'fks-tabs__item',
  'fks-button',
  'fks-dropdown-menu__item',
  'fks-select-dropdown__item',
  'fks-radio-button',
  'tags-view-item',
  'fks-radio-button__inner'
]
//判断用户点击内容是否为需要行为采集的功能
export function judge(val) {
  let y = false
  val.forEach((item) => {
    if (
      classList.includes(item)
    ) {
      y = true
    }
  })
  return y
}